package tutorial.gae;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;

public class ContactDao {

	public static final String ENTITY = "Contact";

	public Contact get(long id) throws EntityNotFoundException {
		Entity e = DatastoreServiceFactory.getDatastoreService().get(
				KeyFactory.createKey(ENTITY, id));

		Contact c = new Contact();
		c.setAddress((String) e.getProperty("adress"));
		c.setFirstName((String) e.getProperty("firstName"));
		c.setLastName((String) e.getProperty("lastName"));
		c.setMemo((String) e.getProperty("memo"));
		c.setPhone((String) e.getProperty("phone"));
		c.setKey(e.getKey());

		return c;

	}

	public Contact create(Contact request) {
		
		Entity newer = new Entity(ENTITY);
		newer.setProperty("address", request.getAddress());
		newer.setProperty("firstName", request.getFirstName());
		newer.setProperty("lastName", request.getLastName());
		newer.setProperty("memo", request.getMemo());
		newer.setProperty("phone", request.getPhone());
		DatastoreServiceFactory.getDatastoreService().put(newer);
		return request;

	}

}
